home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 248_01 / dmerge.c < prev    next >
Text File  |  1989-08-16  |  6KB  |  286 lines

  1. /*    DMERGE:    Dictionary Merge Utility for MicroSPELL 1.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987 by Daniel Lawrence
  5.         All Rights Reserved
  6.  
  7.     Revision History:
  8.  
  9. */
  10.  
  11. #define    maindef    1
  12.  
  13. #include    <stdio.h>
  14. #include    "dopt.h"
  15. #include    "dstruct.h"
  16. #include    "ddef.h"
  17.  
  18. /*    GLOBALS for DMERGE    */
  19.  
  20. char dname[NSTRING];        /* main output dictionary text file */
  21. int upflag;            /* processing upper case flag */
  22.  
  23. main(argc, argv)
  24.  
  25. int argc;    /* command line argument count */
  26. char **argv;    /*              argument vector */
  27.  
  28. {
  29.     WORD *tword;
  30.  
  31.     /* check to see if they need help.... */
  32.     if (argc < 3) {
  33.         usage();
  34.         exit(EXBADOPT);
  35.     }
  36.  
  37.     /* announce us */
  38.     printf("DMERGE %s    Dictionary merge Utility\n", VERSION);
  39.  
  40.     /* grab the output dictionary name */
  41.     --argc;
  42.     argv++;
  43.     strcpy(dname, argv[0]);
  44.  
  45.     /* and read in the user word lists */
  46.     while (--argc) {
  47.         argv++;        /* skip to next argument */
  48.         uread(argv[0]);    /* and read the dictionary in */
  49.     }
  50.  
  51.     /* prepare to merge */
  52.     comsort();
  53.     if (numfiltr)
  54.         printf("%u User words loaded\n", numfiltr);
  55.     else {
  56.         printf("%%No user words loaded\n");
  57.         exit(EXBADOPT);
  58.     }
  59.  
  60.     if (mopen() == FALSE)
  61.         exit(EXMDICT);
  62.  
  63.     /* open the output dictionary file */
  64.     if ((outfile = fopen(dname, "w")) == NULL) {
  65.         printf("%%Can not open output dictionary file\n");
  66.         exit(EXTEMP);
  67.     }
  68.  
  69.     /* and do the merge */
  70.     printf("[Merging....]");
  71.     merge();
  72.     printf("\n%u words merged ... new dictionary is %u words long\n",
  73.             numfiltr, totwords);
  74.  
  75.     mclose();
  76.     fclose(outfile);
  77.     exit(EXGOOD);
  78. }
  79.  
  80. usage()        /* print the command line usage */
  81.  
  82. {
  83.     printf("DMERGE:    Dictionary Merge Utility\n");
  84.     printf("    for MicroSPELL %s\n",VERSION);
  85.     puts("\nUsage\n");
  86.     puts("    dmerge <output dictionary> <user file> {<user file>..}\n");
  87. }
  88.  
  89. int merge()        /* do a merge run against the main dictionary    */
  90.  
  91. {
  92.     register char **curword;/* ptr to current word */
  93.     register int cmp;    /* result of comparison */
  94.     char mword[NSTRING];    /* current dictionary word */
  95.  
  96.     /* make sure the common word list ends with hivalue */
  97.     strcpy(cword[numfiltr], hivalue);
  98.     upflag = TRUE;
  99.  
  100. printf("Scanning....\n");
  101. do
  102. strcpy(mword, nxtmword());
  103.  while (*mword != '{');
  104. printf("past the junk!!!\n");
  105.  
  106.     /* start with the first word in each list */
  107.     strcpy(mword, nxtmword());
  108.     curword = &cword[0];
  109.  
  110.     while (strcmp(*curword, hivalue) < 0 || strcmp(mword, hivalue) < 0) {
  111.  
  112.         /* compare a dictionary word and a user word */
  113.         cmp = strcmp(*curword, mword);
  114.  
  115.         /* and write out the appropriate one */
  116.         if (cmp > 0) {
  117.             outword(mword);
  118.             strcpy(mword, nxtmword());
  119.         } else if (cmp < 0) {
  120.             outword(*curword);
  121.             curword++;
  122.         } else {
  123.             outword(*curword);
  124.             strcpy(mword, nxtmword());
  125.             curword++;
  126.         }
  127.     }
  128. }
  129.  
  130. outword(word)    /* output a word to the new main dictionary text file */
  131.  
  132. char *word;    /* word to write to dictionary */
  133.  
  134. {
  135.     /* if this is the seperator character, toggle uppercase flag off */
  136.     if (*word == SEPCHAR)
  137.         upflag = FALSE;
  138.  
  139.     /* If we switch from upper to lower case with no seperator,
  140.        insert one */
  141.     if (upflag)
  142.         if (islower(*word)) {
  143.             fprintf(outfile, "%c\n", SEPCHAR);
  144.             upflag = FALSE;
  145.         }
  146.  
  147.     /* and finally, dump the word out */
  148.     fprintf(outfile, "%s\n", word);
  149.     totwords++;
  150. }
  151.  
  152. #if    RAMSIZE & LATTICE & MSDOS
  153. /*    These routines will allow me to track memory usage by placing
  154.     a layer on top of the standard system malloc() and free() calls.
  155.     with this code defined, the number of allocated bytes is displayed
  156.     in the upper right corner of the screen
  157. */
  158.  
  159. #undef    malloc
  160. #undef    free
  161.  
  162. char *allocate(nbytes)    /* allocate nbytes and track */
  163.  
  164. unsigned nbytes;    /* # of bytes to allocate */
  165.  
  166. {
  167.     char *mp;    /* ptr returned from malloc */
  168.     char *malloc();
  169.  
  170.     mp = malloc(nbytes);
  171.     if (mp) {
  172.         envram += nbytes;
  173. #if    RAMSHOW
  174.         dspram();
  175. #endif
  176.     }
  177.  
  178.     return(mp);
  179. }
  180.  
  181. release(mp)    /* release malloced memory and track */
  182.  
  183. char *mp;    /* chunk of RAM to release */
  184.  
  185. {
  186.     unsigned *lp;    /* ptr to the long containing the block size */
  187.  
  188.     if (mp) {
  189.         lp = ((unsigned *)mp) - 1;
  190.  
  191.         /* update amount of ram currently malloced */
  192.         envram -= (long)*lp - 2;
  193.         free(mp);
  194. #if    RAMSHOW
  195.         dspram();
  196. #endif
  197.     }
  198. }
  199.  
  200. #if    RAMSHOW
  201. dspram()    /* display the amount of RAM currently malloced */
  202.  
  203. {
  204.     char mbuf[20];
  205.     char *sp;
  206.  
  207. /*    TTmove(term.t_nrow - 1, 70);*/
  208.     sprintf(mbuf, "[%lu]", envram);
  209.     sp = &mbuf[0];
  210.     puts(sp);
  211. }
  212. #endif
  213. #endif
  214.  
  215. #if    AZTEC & MSDOS
  216. #undef    fgetc
  217. /*    a1getc:        Get an ascii char from the file input stream
  218.             but DO NOT strip the high bit
  219. */
  220.  
  221. int a1getc(fp)
  222.  
  223. FILE *fp;
  224.  
  225. {
  226.     int c;        /* translated character */
  227.  
  228.     c = getc(fp);    /* get the character */
  229.  
  230.     /* if its a <LF> char, throw it out  */
  231.     while (c == 10)
  232.         c = getc(fp);
  233.  
  234.     /* if its a <RETURN> char, change it to a LF */
  235.     if (c == '\r')
  236.         c = '\n';
  237.  
  238.     /* if its a ^Z, its an EOF */
  239.     if (c == 26)
  240.         c = EOF;
  241.  
  242.     return(c);
  243. }
  244. #endif
  245.  
  246. perform(cmd)    /* send out a DOS command and execute it */
  247.  
  248. char *cmd;    /* command to execute */
  249.  
  250. {
  251.     if (swdebug)    /* output command string with diagnostics */
  252.         puts(cmd);
  253.  
  254. #if    LATTICE & ~CMS
  255.     forkl(getenv("COMSPEC"),"command","-C",cmd,NULL);
  256.     return(wait());
  257. #endif
  258.  
  259. #if    (AZTEC & ~AMIGA) | CMS
  260.     system(cmd);
  261.     return(TRUE);
  262. #endif
  263.  
  264. #if    AZTEC & AMIGA
  265.     fexecl(cmd, NULL);
  266.     return(wait());
  267. #endif
  268. }
  269.  
  270. #if    CMS
  271. #undef    fopen
  272. /*    The IBM 30xx likes to tell us when file opens
  273.     fail...it's too chatty....I like to handle these myself    */
  274.  
  275. FILE *cmsopen(file, mode)
  276.  
  277. char *file;    /* name of file to open */
  278. char *mode;    /* mode to open it in */
  279.  
  280. {
  281.     quiet(1);
  282.     return(fopen(file,mode));
  283. }
  284. #endif
  285.  
  286.